﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetfractionHub.Network.Protocol;
using NetfractionHub.Network.Protocol.BASE.Factory;
using NetfractionHub.Network.Protocol.UCMD;
using NetfractionHub.Network.Protocol.UCMD.Factory;

namespace NetfractionHub.LocalMachine.BotCommands.Handler
{
    [CommandHandler("getclientid", TriggerAlias="getcid")]
    [UserCommand("Get Client ID", FactoryCMD.ConextTypesEnum.HubCommand, "HMSG {0}getcid\\\\s\"%[line:Nickname]\"\n",
        Presentation = "<Canvas Width=\"298\" Height=\"476\"><Canvas.Grid><ColumnDefinitions><ColumnDefinition Mode=\"Fill\" Alignment=\"TopLeft\" /></ColumnDefinitions><RowDefinitions><RowDefinition /><RowDefinition /></RowDefinitions></Canvas.Grid><GroupBox Title=\"Command Type\" Grid.Row=\"1\" Grid.Column=\"1\"><GroupBox.Grid><ColumnDefinitions><ColumnDefinition Mode=\"Fill\" Alignment=\"TopLeft\" /><ColumnDefinition Mode=\"Fill\" Alignment=\"BottomRight\" /></ColumnDefinitions><RowDefinitions><RowDefinition /><RowDefinition /></RowDefinitions></GroupBox.Grid><Content><RadioButton Grid.Row=\"1\" Grid.Column=\"1\" Value=\"0\" Content=\"Separator\" /><RadioButton Grid.Row=\"2\" Grid.Column=\"1\" Value=\"1\" Content=\"Raw\" /><RadioButton Grid.Row=\"1\" Grid.Column=\"2\" Value=\"2\" Content=\"Chat\" /><RadioButton Grid.Row=\"2\" Grid.Column=\"2\" Value=\"3\" Content=\"PM\" /></Content></GroupBox><GroupBox Title=\"Context\" Grid.Row=\"2\" Grid.Column=\"1\"><GroupBox.Grid><ColumnDefinitions><ColumnDefinition Mode=\"Fill\" Alignment=\"TopLeft\" /><ColumnDefinition Mode=\"Fill\" Alignment=\"BottomRight\" /></ColumnDefinitions><RowDefinitions><RowDefinition /><RowDefinition /></RowDefinitions></GroupBox.Grid><Content><CheckBox Grid.Row=\"1\" Grid.Column=\"1\" Value=\"0\" Content=\"Hub Menu\" /><CheckBox Grid.Row=\"2\" Grid.Column=\"1\" Value=\"1\" Content=\"User Menu\" /><CheckBox Grid.Row=\"1\" Grid.Column=\"2\" Value=\"2\" Content=\"Search Menu\" /><CheckBox Grid.Row=\"2\" Grid.Column=\"2\" Value=\"3\" Content=\"Filelist Menu\" /></Content></GroupBox></Canvas>")]
    public class GetClientId : CommandHandler
    {
        public override BotReturnStatus HandleCommandMessage(NetfractionHub.RemoteMachine.Node source, CommandMessage message)
        {
            if (message.ArgumentsCount == 0)
            {
                if (Security.Permission.HasPermission(source.SecurityIdentity, typeof(Security.Permissions.GetOwnClientIdPermission)))
                    return new BotReturnStatus(true, string.Format("Your Client Id is: {0}",
                        !string.IsNullOrEmpty(source.Identity.ClientId) ? source.Identity.ClientId : "unknown"));
                else
                    return new BotReturnStatus(false, Security.Permission.GetDeniedString(typeof(Security.Permissions.GetOwnClientIdPermission)));
            }
            else if (message.StringArguments.Count == 1 && message.StringArguments.ContainsKey("user") || message.UnnamedArguments.Count == 1)
            {
                string user = message.StringArguments.Count == 1 ? message.StringArguments["user"] : message.UnnamedArguments[0];
                if (Security.Permission.HasPermission(source.SecurityIdentity, typeof(Security.Permissions.GetIdentityClientIdPermission)))
                    return GetIdentityClientId(source, user);
                else
                    return new BotReturnStatus(false, Security.Permission.GetDeniedString(typeof(Security.Permissions.GetIdentityClientIdPermission)));
            }
            else
                return new BotReturnStatus(false);
        }

        private BotReturnStatus GetIdentityClientId(NetfractionHub.RemoteMachine.Node source, string user)
        {
            RemoteMachine.Node nodeToFind = Network.ConnectionManager.GetConnectedNodeByNickname(user);
            if (nodeToFind == null)
                return new BotReturnStatus(false, string.Format("Cannot get Client Id, the user '{0}' does not exist.", user));

            if (!Security.RoleManager.IsInAncestorTree(source.SecurityIdentity.Role, nodeToFind.SecurityIdentity.Role))
                return new BotReturnStatus(false, string.Format("Permission denied. You are not an ancestor of user '{0}'.", user));

            return new BotReturnStatus(true, string.Format("'{0}' has the following Client Id: {1}", nodeToFind.Identity.Nickname,
                !string.IsNullOrEmpty(nodeToFind.Identity.ClientId) ? nodeToFind.Identity.ClientId : "unknown"));
        }
    }
}
